<!-- 生成订单 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建订单--书瞳</title>
    <link rel="icon" href="__ROOT__/public/lcbook.ico" type="image/x-icon">
    <link rel="stylesheet" href="__ROOT__/public/static/css/common.css">
    <link rel="stylesheet" href="__ROOT__/public/static/css/header.css">
    <link rel="stylesheet" href="__ROOT__/public/static/css/popBox.css">
    <link rel="stylesheet" href="__ROOT__/public/static/css/chooseCustomerPop.css">

    <link rel="stylesheet" href="__ROOT__/public/static/css/cartnav.css">
    <link rel="stylesheet" href="__ROOT__/public/static/css/corder.css">
    <!-- fontawesome字体 -->
    <link rel="stylesheet" href="__ROOT__/public/static/font/font-awesome.min.css">
</head>

<body>
    <!-- 头部 -->
    {include file="./public/common/header.html"/}

    <!-- 中间logo部分 -->
    <div id="nav">
        <div class="navBox">
            <div class="logo">
                <img src="__ROOT__/public/static/image/logo.png" alt="">
            </div>
            <span class="title">购物车</span>
            <ul class="bar"
                style="background:  url(__ROOT__/public/static/image/procedure_bg2.png) no-repeat center center;">
                <li>我的购物车</li>
                <li class="lichoose">填写订单</li>
                <li>完成订单</li>
            </ul>
        </div>
    </div>

    <!-- 我的订单 -->
    <div id="order">
        <div class="orderBox">
            <!-- 上面部分 -->
            <div class="up">
                <ul>
                    <li class="jcstart"><a href="{:url('cart/cart')}">
                            < </a>
                    </li>
                </ul>

                <!-- 收货人 -->
                <!-- 有收货人，但是不是默认地址 -->

                <!-- 得到收货人的数量，如果不为0肯定是默认地址 -->
                {assign name="count" value="$Customers|count"}

                <!-- 如果收货人数量不为0并且第一个是不是默认的 -->
                {if condition="$count != 0"}
                <!-- 有收货人 但不是默认 -->
                <ul id="custnodefault" class="customer mb12 displayNone">
                    <li>
                        <div>
                            <span class="k">收货人：</span>
                            <span class="v name"></span>
                        </div>
                        <div>
                            <span class="k">收货地址：</span>
                            <span class="v address"></span>
                        </div>
                        <div>
                            <span class="k">联系电话：</span>
                            <span class="v phone"></span>
                        </div>
                    </li>

                    <!-- 操作 -->
                    <li>
                        <a href="#" class="mr20 setDefault">设为默认</a>
                        <a href="#" class="mr20 changeCustomer">更改</a>
                    </li>
                </ul>

                <!-- 有收货人，并且是默认地址 -->
                <ul id="custdefault" class="customer mb12">
                    <li>
                        <div>
                            <span class="k">收货人：</span>
                            <span class="v name">{$Customers[0]['sname']}</span>
                            <span class="default">默认</span>
                        </div>
                        <div>
                            <span class="k">收货地址：</span>
                            <span class="v address">{$Customers[0]['address']}</span>
                        </div>
                        <div>
                            <span class="k">联系电话：</span>
                            <span class="v phone">{$Customers[0]['mobile']}</span>
                        </div>
                    </li>

                    <!-- 操作 -->
                    <li>
                        <a href="#" class="changeCustomer">更改</a>
                    </li>
                </ul>

                <!-- 无收货人 -->
                <ul class="customer mb12 displayNone">
                    <li class="noCustomer">
                        <div>
                            <span class="activate_font k">&#xf196</span>
                            <span class="activate_font v">您还没有收货人信息哦!赶快添加一个吧~~</span>
                        </div>
                    </li>
                </ul>

                {else /}
                <!-- 无收货人 -->
                <ul class="customer mb12">
                    <li class="noCustomer">
                        <div onclick="addCustomer()">
                            <span class="activate_font k">&#xf196</span>
                            <span class="activate_font v">您还没有收货人信息哦!赶快添加一个吧~~</span>
                        </div>
                    </li>
                </ul>
                {/if}

                <!-- 选择收货人的弹出面板 -->
                <div id="chooseCustomer" class="popBox" style="display: none;">
                    <div class="popTitle">
                        <span>选择收货人</span>
                        <div>
                            <span><a href="#" class="editPop">编辑 &#xf040</a></span>
                            <span><a href="#" class="dropPop">取消 &#xf00d</a></span>
                            <span><a href="#" class="okPop">确定 &#xf00c</a></span>
                        </div>
                    </div>
                    <div class="popContent">
                        {if condition="$Customers neq '' "}
                        {foreach $Customers as $Customer}
                        {if condition="$Customer.cdefault eq '1' "}
                        <!-- 默认收货人 -->
                        <div id="{$Customer.custID}" class="chooseCustomerBox chooseCustomerBox_activate"
                            data-custid="{$Customer.custID}" data-cdefault="{$Customer.cdefault}">
                            <div class="chooseCustomerBoxTitle">
                                <span class="name">{$Customer.sname}</span>
                                <span class="phone">{$Customer.mobile}</span>
                            </div>
                            <p class="address">{$Customer.address}</p>
                            <p>&#xf041 默认</p>
                        </div>
                        {else /}
                        <!-- 收货人 -->
                        <div id="{$Customer.custID}" class="chooseCustomerBox" data-custid="{$Customer.custID}"
                            data-cdefault="{$Customer.cdefault}">
                            <div class="chooseCustomerBoxTitle">
                                <span class="name">{$Customer.sname}</span>
                                <span class="phone">{$Customer.mobile}</span>
                            </div>
                            <p class="address">{$Customer.address}</p>
                        </div>
                        {/if}
                        {/foreach}
                        {/if}
                    </div>
                </div>


                <!-- 商品 -->
                {assign name="sum" value="0"}
                {foreach $carts as $cart}
                {assign name="sum" value="$sum + $cart.yourprice * $cart.num"}
                <ul class="shop">
                    <li class="jcstart f333"><img src="__ROOT__/public/static/image/{$cart.pictures}" alt=""><span
                            class="mt12">{$cart.bname}</span></li>
                    <li class="mt12">￥{$cart.yourprice}</li>
                    <li class="mt12 activate_font">×{$cart.num}</li>
                    <li class="mt12 f333">￥{$cart.yourprice * $cart.num}</li>
                </ul>
                {/foreach}
            </div>

            <!-- 下面部分 -->
            <div class="down">
                <div class="priceBox">
                    <p>
                        <span>总计（不含运费）：</span>
                        <span id="sum" class="activate_font fs20">￥{$sum+6.00}</span>
                    </p>

                    <p>
                        <span>运费：</span>
                        <span>￥6.00</span>
                    </p>
                </div>
                <a href="#"><button id="orderSubmit">提交订单</button></a>
            </div>
        </div>
    </div>

    <!-- js代码 -->
    <script src="__ROOT__/public/static/js/jquery-1.8.3.min.js"></script>
    <script src="__ROOT__/public/static/js/header.js"></script>
    <script>
        $(function () {
            var custID = -1;//标记无收货人
            if (!$("#custdefault").hasClass('displayNone')) { //有收货人
                custID = $('.chooseCustomerBox_activate').data('custid');
            }
            console.log(custID);

            //更改收货人
            $('.changeCustomer').click(function () {
                showChooseCustomerPlane();
                //设置选择的收货人
                $('.chooseCustomerBox').each(function () {
                    if ($(this).data('custid') == custID) {
                        $(this).addClass('chooseCustomerBox_activate');
                    } else {
                        $(this).removeClass('chooseCustomerBox_activate');
                    }
                });
            });

            //编辑收货人 跳转到编辑界面
            $('.editPop').click(function () {
                if (confirm("需要跳转编辑收货人界面吗?")) {
                    window.location.href = "__ROOT__/index/consignee/consignee";
                }
            });

            //选择收货人完毕
            /*
                1、获取用户选择的收货人
                2、设置收货人为此orderID
            */
            $('.okPop').click(function () {
                let custid;
                let cdefault;
                $('.chooseCustomerBox').each(function () {
                    if ($(this).hasClass('chooseCustomerBox_activate')) {
                        custid = $(this).data('custid');
                        cdefault = $(this).data('cdefault');
                    }
                });
                // console.log(cdefault);
                $('.customer').addClass("displayNone");
                let id = '';
                if (cdefault == 1) { //如果选择的是默认地址
                    id = '#custdefault';
                } else { //其他地址
                    id = '#custnodefault';
                }
                // console.log($(id).get(0));
                $(id).removeClass("displayNone");
                $(id + ' .name').text($('#' + custid + ' .name').text());
                $(id + ' .address').text($('#' + custid + ' .address').text());
                $(id + ' .phone').text($('#' + custid + ' .phone').text());
                hideChooseCustomerPlane();
                custID = custid;
            });

            //某个收货人被选择
            $('.chooseCustomerBox').click(function () {
                let custid = $(this).data('custid');
                $('.chooseCustomerBox').removeClass('chooseCustomerBox_activate');
                $('#' + custid).addClass('chooseCustomerBox_activate');
            })

            //设为默认按钮被点击
            $('.setDefault').click(function () {
                $.get("{:url('consignee/setDefault')}", {
                    custID: custID
                }, function () {
                    window.location.reload();
                });
            });


            // 当取消的时候重新设置可以滚动
            $('.dropPop').click(function () {
                $('body').css('overflow', 'scroll');
            });


            //提交订单
            /*
                需要获取的信息：
                email   购买的账号   在session中
                custID  顾客Id     全局变量
                shifu   总金额
                custIDs 当前订单所含的购物车商品
            */
            $('#orderSubmit').click(function () {
                let shifu = parseFloat($("#sum").text().substring(1));
                $.post("{:url('forder/addOrder')}", {
                    shifu: shifu,
                    custID: custID
                }, function (res) {
                    if (res == "error") {
                        alert("系统错误");
                        return;
                    }
                    window.location.href = "__ROOT__/index/forder/forder?orderID=" + res;
                })
                // console.log(custID);
            });
        });

        // 显示选择收货人面板
        function showChooseCustomerPlane() {
            $('.outsideLayer').show();
            $('#chooseCustomer').show();
            $('body').css('overflow', 'hidden');
        }

        //隐藏显示收货人面板
        function hideChooseCustomerPlane() {
            $('.outsideLayer').hide();
            $('#chooseCustomer').hide();
            $('body').css('overflow', 'scroll');
        }

        //添加收货人被点击
        function addCustomer() {
            if (confirm("将跳转编辑收货人界面，要继续吗？")) {
                window.location.href = "__ROOT__/index/consignee/consignee";
            }
        }
    </script>

</body>

</html>